package org.compiere.process;

import java.awt.Container;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import org.compiere.apps.IProcessParameter;
import org.compiere.apps.ProcessCtl;
import org.compiere.apps.ProcessParameterPanel;
import org.compiere.apps.Waiting;
import org.compiere.model.Mhrmemployee;
import org.compiere.model.Mhrmorder;
import org.compiere.model.X_hrm_order;
import org.compiere.util.ASyncProcess;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;

/**
 * Print (display) order of choice
 * @author V.Sokolov
 *
 */
public class OrderPrint extends SvrProcess implements ASyncProcess {

	/**/
	private int AD_Process_ID;
	/**/
	private Mhrmorder hrmorder = null;
	/**	Employee Lines	*/
	private Mhrmemployee[] linesEmployee;
	/**/
	ASyncProcess m_parent;
	
	@Override
	protected void prepare() {
		
		String orderType = "";		
		hrmorder = new Mhrmorder(Env.getCtx(), getRecord_ID(), null);
		
		if(hrmorder == null) return;
		// hiring
		if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Hiring)){
			orderType = "hiring";
		}
		// moving
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Moving)){
			orderType = "moving";
		}
		// Business Trip
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_BusinessTrip)){
			linesEmployee = hrmorder.getLinesEmployee();
			if(linesEmployee.length > 1)
				orderType = "BusinessTripMulti";
			else
				orderType = "BusinessTripSingle";
		}
		else if (hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_BossTrip)){
			orderType = "BossTripS";
		}
		else if (hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_BossVacation)){
			orderType = "BossVacation";
		}
		// vacation
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Vacation)){
			if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_NormVacation) 
					|| hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_NonNormVacation))
				orderType = "vacation";
			else if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_EducationLeave))
				orderType = "educationLeave";
			else if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_Pregnancy))
				orderType = "pregnancy";
			else if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_LeaveToCareForAChild))
				orderType = "childcare";
			else if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_LeaveWithoutSalary))
				orderType = "leavesalary";
		}
		// extension vacation period
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Vacationadd)){
			if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_NormVacation)
					|| hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_NonNormVacation))
					orderType = "vacation";
			else if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_EducationLeave))
					orderType = "educationLeave";
			else if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_Pregnancy))
					orderType = "pregnancy";
			else if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_LeaveToCareForAChild))
					orderType = "extchild";
			else if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_LeaveWithoutSalary))
					orderType = "leavesalary";
		}		
		// dismissal
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Dismissal)){
			orderType = "dismissal";
		}
		// withholding
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_StoppageOfWages)){
			orderType = "withholding";
		}
		// premium
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Premium)){
			orderType = "premium";
		}
		// penalty
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Penalty)){
			orderType = "penalty";
		}
		// Assignment
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Assignment)){
			orderType = "assignment";
		}
		// Review vacation
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Review)){
			orderType = "reviewvacation";
		}
		// Material aid
		else if(hrmorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Materialaid)){
			orderType = "materialaid";
		}
		
		AD_Process_ID = Util.getAD_Process(orderType);

	}

	@Override
	protected String doIt() throws Exception {
		
		if(AD_Process_ID == 0)
			return  "The process can not be found";
		//  Prepare Process
		ProcessInfo pi = new ProcessInfo ("The printing process of the order", AD_Process_ID);
		pi.setAD_User_ID (Env.getAD_User_ID(Env.getCtx()));
		pi.setAD_Client_ID(Env.getAD_Client_ID(Env.getCtx()));

		List<ProcessInfoParameter> po = new ArrayList<ProcessInfoParameter>();
		po.add(new ProcessInfoParameter("hrm_order_ID",hrmorder.get_ID(),null,"",""));
		if(hrmorder.gethrm_workleavetype()!=null){
			if(hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_NormVacation)
					|| hrmorder.gethrm_workleavetype().equals(X_hrm_order.HRM_WORKLEAVETYPE_NonNormVacation))
			{
				ProcessInfoParameter p = new ProcessInfoParameter("hrm_order_id",hrmorder.get_ID(),null,"","");
					po.add(p);
			}
		}
		po.add(new ProcessInfoParameter("orderType",hrmorder.gethrm_ordertype(),null,"",""));
		po.add(new ProcessInfoParameter("tripType",hrmorder.gethrm_businesstriptype(),null,"",""));		
		//
		ProcessInfoParameter[] pp = new ProcessInfoParameter[po.size()];
		po.toArray(pp);
		pi.setParameter(pp);
		//	Execute Process
		ProcessParameterPanel pu = new ProcessParameterPanel(0, pi);
		ProcessCtl.process(this, 0, (IProcessParameter) pu, pi, null);
		
		return "View the report";
	}

	private Waiting         m_waiting;
	
	@Override
	public void lockUI(ProcessInfo pi) {
		
		JFrame frame = Env.getFrame((Container)m_parent);
		m_waiting = new Waiting (frame, Msg.getMsg(Env.getCtx(), "Processing"), false, pi.getEstSeconds());
		
	}

	@Override
	public void unlockUI(ProcessInfo pi) {

		if (m_waiting != null)
			m_waiting.dispose();
		m_waiting = null;
	}

	@Override
	public boolean isUILocked() {
		return false;
	}

	@Override
	public void executeASync(ProcessInfo pi) {
		
	}

}
